package cn.bluemobi.dylan.step.ui.my.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpFragment;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Score;
import cn.bluemobi.dylan.step.moder.bean.StepData;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.my.ScoreListContract;
import cn.bluemobi.dylan.step.presenter.my.fragment.ScoreListPresenter;
import cn.bluemobi.dylan.step.ui.my.activity.ScoreActivity;
import cn.bluemobi.dylan.step.ui.my.adapter.ScoreAdapter;

/**
 * Created by $Liudan on 2018/11/21 0021.
 */
public class ScoreListFragment extends BaseMvpFragment<ScoreListPresenter> implements ScoreListContract.View {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private User user;
    private MemberAccount memberAccount; //积分
    private ScoreAdapter adapter;
    private List<Score> scoreList;
    private ScoreActivity scoreActivity;
    private String tab = ScoreActivity.tab1;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.my_score_list;
    }

    @Override
    protected void initEventAndData() {
        tab = getArguments().getString("tab", ScoreActivity.tab1);
        initView();
        mPresenter.preUser();
        initRefreshLoadMore();
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
    }

    private void initRefreshLoadMore() {
//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshLayout) {
//                if(user!=null){
//                    mPresenter.httpScoreList(user.memberId,true);
//                }else {
//                    mPresenter.preUser();
//                }
//            }
//        });
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (user != null) {
                    mPresenter.httpScoreList(user.memberId, false,tab);
                }
            }
        });
//        refreshLayout.autoRefresh();
    }

    public void refreshScorList() {
        if (user != null) {
            mPresenter.httpScoreList(user.memberId, true,tab);
        } else {
            mPresenter.preUser();
        }
    }

    private void setAdapterData() {
        if (scoreList != null && scoreList.size() > 0) {
            if (adapter == null) {
                adapter = new ScoreAdapter(getActivity(), scoreList);
                recyclerView.setAdapter(adapter);
            } else {
                adapter.notifyChanged(scoreList);
            }
            adapter.setOnItemOnClilcklisten(new OnItemOnClilcklisten());
        }
    }

    private class OnItemOnClilcklisten implements ScoreAdapter.OnItemOnClicklisten {


        @Override
        public void onItemClick(View v, int position) {

        }

    }

    @Override
    public void showUserInfo(User user) {
        this.user = user;
        if (user != null) {
            if(scoreActivity==null){
                scoreActivity = (ScoreActivity)getActivity();
            }
            scoreActivity.showDialog();
//            mPresenter.httpScoreList(user.memberId, true);
        }
    }

    @Override
    public void showStepcount(StepData stepData) {
    }

    @Override
    public void showAccountScore(MemberAccount memberAccount) {
        this.memberAccount = memberAccount;
    }

    @Override
    public void httpScoreList(List<Score> result) {
        this.scoreList = result;
        setAdapterData();
    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void stateError() {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {
//        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if(scoreActivity==null){
            scoreActivity = (ScoreActivity)getActivity();
        }
        scoreActivity.closeDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


}
